<template>
   <div class="app-container">

     <div class="app-container__section" >
       <PlusSearch
         v-bind="searchBinds"
         :columns="columns"
         v-model="gSearchForm"
       >
       </PlusSearch>
     </div>
     <div class="app-container__section">
       <PlusTable  v-bind="tableBinds" v-if="refreshTable" :default-expand-all="isExpandAll"    :is-selection="false"  :has-index-column="false" row-key="menuId" :columns="columns" >
         <template #title>
           <table-title />
         </template>
         <template #toolbar>
           <el-button
             type="primary"
             plain
             icon="Plus"
             @click="handleAdd"
             v-hasPermi="['system:menu:add']"
           >新增</el-button>
           <el-button
             type="info"
             plain
             icon="Sort"
             @click="toggleExpandAll"
           >展开/折叠</el-button>
           <ClockTimer :task="gGetList" />
           <SearchAreaControl v-model="gShowSearchForm" />
         </template>
         <template  #plus-cell-icon="scope">
           <svg-icon :icon-class="scope.row.icon" />
         </template>
         <template #plus-cell-status="scope">
           <dict-tag :options="dictData.sys_normal_disable" :value="scope.row.status" />
         </template>
         <template #plus-cell-action="scope">
             <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:menu:edit']">修改</el-button>
             <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:menu:add']">新增</el-button>
             <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:menu:remove']">删除</el-button>
         </template>
       </PlusTable>
     </div>

      <!-- 添加或修改菜单对话框 -->
     <AddForm ref="addFormRef" @refreshList="gGetList" />

   </div>
</template>

<script setup >
import SvgIcon from "@/components/SvgIcon";
import { PlusSearch, PlusTable } from 'plus-pro-components'
import { useColumnsDict, useCommonConfig } from './hook.js'
import { useDelHook, useListHook } from '@/hooks/crud/index.js'
import { useListConfigHook } from '@/hooks/usePlusConfigHook.js'
import AddForm from "./addForm.vue"
defineOptions({
  name: 'Menu'
})
const { proxy,apiFn,primaryKey,moduleName,beforeDelFn,getTreeSelect, menuOptions} = useCommonConfig()
const { dictData, columns, } = useColumnsDict()

// ====================列表逻辑=========================
const gSearchForm = ref({  })
const { gGetList, gAllExposeOption, } = useListHook({
  listApiFn: apiFn.listApiFn,
  gSearchForm,
  hasPage:false,
  beforeRequest: (reqData) => {
    console.log(reqData)
    return reqData
  },
  afterRequest: (resData) => {
    return {
      list:proxy.handleTree(resData,'menuId')
    }
  }
})
// ====================属性绑定=========================
const { gTableBinds, gSearchBinds, gSelection, gShowSearchForm } = useListConfigHook()
const tableBinds = computed(() => gTableBinds({ ...gAllExposeOption }))
const searchBinds = computed(() => gSearchBinds({ ...gAllExposeOption } ))
// ==================删除逻辑===========================
const { gDelete } = useDelHook({
  delApiFn: apiFn.delApiFn,
  moduleName,
  beforeDelFn: (reqData) => beforeDelFn(reqData),
  afterDelFn: (resData) =>gGetList(),
})
const handleDelete = (data)=>{
  gDelete(data)
}
// ===================新增、修改======================
const addFormRef = ref(null)
// 新增操作
function handleAdd(row) {
  addFormRef.value.gOpen({ parentId:row.menuId || 0 })
}
// 修改操作
function handleUpdate(row) {
  addFormRef.value.gOpen(row)
}

const refreshTable = ref(true);

const isExpandAll = ref(false);
/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}
</script>
